//
//  CCStatusCell.swift
//  CC-SinaWeibo
//
//  Created by Chenchen on 16/2/21.
//  Copyright © 2016年 Chenchen. All rights reserved.
//

import UIKit
import SnapKit

class CCStatusCell: UITableViewCell {

    /// 添加约束对象属性,用于使用的记录
    var bottomViewTopConstaint: Constraint?
    
    // 添加模型属性
    var status: CCStatus? {
        didSet {
            // 设置原创微博的模型数据
            statusOriginalView.status = status
            // 打印 imageURLs 
//            print("转发微博文字=====>",status?.retweeted_status?.text)
            // 设置转发微博的数据模型
         statusRetweetedView.retweetedStatus = status?.retweeted_status
            // 卸载约束对象
            self.bottomViewTopConstaint?.uninstall()
            // 根据数据调整约束对象
            if let re = status?.retweeted_status {
                // 给转发微博设置数据模型
                statusRetweetedView.retweetedStatus = re
                statusCellBottomView.snp_makeConstraints { (make) -> Void in
                    // 添加约束对象 ,并记录约束对象
                    self.bottomViewTopConstaint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                }
                // 显示转发微博视图
                statusRetweetedView.hidden = false
            } else {  // 原创微博
                statusCellBottomView.snp_makeConstraints { (make) -> Void in
                    // 添加约束对象 ,并记录约束对象
                    self.bottomViewTopConstaint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                }
                // 隐藏转发微博视图
                statusRetweetedView.hidden = true
            }
        }
    }
    
    
    // xib加载视图
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    // cell默认的构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 调用
        setUpUI()
        // 设置cell的样式
        selectionStyle = .None
        
    }
    // 默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 自定义cell
    private func setUpUI() {
        // 添加子视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusCellBottomView)
        contentView.addSubview(statusRetweetedView)
        
        // 设置子视图约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
            // cell的高度
//            make.height.equalTo(80)
        }
        
        // 转发微博约束
        statusRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.equalTo(statusOriginalView.snp_left)
            make.right.equalTo(statusOriginalView.snp_right)
        }
        
        // 底部视图约束
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
            self.bottomViewTopConstaint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            
            //make.top.equalTo(statusRetweetedView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            // 高度
            make.height.equalTo(35)
//            make.bottom.equalTo(contentView.snp_bottom).offset(-10)
        }
        
        // 设置contenView约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(statusCellBottomView.snp_bottom)
            
        }
    }
    
    /// 懒加载子视图
    private lazy var statusOriginalView: CCStatusCellOriginalView = CCStatusCellOriginalView()

    /// 懒加载转发微博部分
    private lazy var statusRetweetedView: CCStatusRetweetedView = CCStatusRetweetedView()
    
    /// 懒加载底部视图
    private lazy var statusCellBottomView: CCStatusCellBottomView = CCStatusCellBottomView()
    
    
}

